import BaseSprite from "../../../Script/Base/BaseSprite";
import { IBaseSkeData } from "../../../Script/Interface/IMessage";
import GameUtil from "../../../Script/Util/GameUtil";
import TaskModule from "../../Common/TaskModule";
import AudioManager from "../../Util/AudioManager";

interface ICartoonConfig {
    id: number;
    from: cc.Vec2;
    to: cc.Vec2;
    time: number;
    animName: string;
    audio?: string;     // 音效
    delay?: number;     // 音效延时
}

const { ccclass, property } = cc._decorator;

// const VIEW_DATA: IViewData = {
//     name: 'CartoonUI',
//     type: EViewType.Panel,
//     bundle: EBundleType.Game,
//     zIndex: 100,
// };

@ccclass
export default class CartoonUI extends BaseSprite {

    @property({type: [cc.Node], tooltip: "卡通动画"})
    private cartoons: cc.Node[] = [];

    @property({ type: [sp.Skeleton], tooltip: "林瑶盖被子" })
    private ske1: sp.Skeleton[] = [];

    @property({ type: [sp.Skeleton], tooltip: "男主穿拖鞋" })
    private ske2: sp.Skeleton[] = [];

    private cartoonConfigs: ICartoonConfig[] = [];
    private curSkes: sp.Skeleton[] = [];
    private callback: Function = null;

    private configs: { id: number, data: ICartoonConfig[] }[] = [
        {
            id: 1,
            data: [
                { id: 1, from: cc.v2(92, -430 + 1000), to: cc.v2(92, -430), time: 0.3, animName: 'fen1', audio: '在床上扯着被子' },
                { id: 2, from: cc.v2(187 + 1000, -416), to: cc.v2(187, -416), time: 0.3, animName: 'fen2', audio: '拍打被子' },
                { id: 3, from: cc.v2(-177 - 1000, -416), to: cc.v2(-177, -416), time: 0.3, animName: 'daiji_Q' },
            ]
        },
        {
            id: 2,
            data: [
                { id: 1, from: cc.v2(-5, 297 + 1000), to: cc.v2(-5, 297), time: 0.3, animName: 'zoulu', audio: '走路拖鞋' },
                { id: 2, from: cc.v2(-377 - 1000, -722), to: cc.v2(-377, -722), time: 0.3, animName: 'fen1', audio: '开门', delay: 100 },
                { id: 3, from: cc.v2(97 + 1000, -710), to: cc.v2(97, -710), time: 0.3, animName: 'fen2' },
            ]
        },
    ];

    private getConfigById(id: number) {
        const config = this.configs.find((o) => o.id === id);
        return config?.data;
    }

    protected onLoad() {
        super.onLoad();

        // 收回任务提示
        TaskModule.instance.hideTask();
    }

    public init(id: number, callback: Function = null) {
        this.callback = callback;
        this.cartoonConfigs = this.getConfigById(id);
        if (this.cartoonConfigs) {
            // 隐藏其他节点
            this.cartoons.forEach((o, i) => {
                o.active = i === id - 1;
            });
            // 设置当前操作动画
            switch (id) {
                case 1:
                    this.curSkes = this.ske1;
                    break;
                case 2:
                    this.curSkes = this.ske2;
                    break;
            }
            // 播放动画
            this.playCartoon();
        } else {
            console.log("CartoonUI.init()===>数据不存在~");
        }
    }

    private playCartoon() {
        this.curSkes.forEach((o, i) => {
            const config = this.cartoonConfigs[i];
            o.node.setPosition(config.from);
            cc.tween(o.node).sequence(
                cc.delayTime(i * 2.5),
                cc.moveTo(config.time, config.to),
                cc.callFunc(async () => {
                    if (config.audio) {
                        setTimeout(() => {
                            AudioManager.instance.playSound(config.audio);
                        }, config.delay || 0);
                    }
                    this.playAnimation(i, config);
                }),
            ).start();
        });
    }

    private playAnimation(index: number, config: ICartoonConfig) {
        const data: IBaseSkeData = {
            ske: this.curSkes[index],
            animName: config.animName,
            isLoop: false,
            callback: () => {
                if (index === this.curSkes.length - 1) {
                    this.callback && this.callback();
                    this.callback = null;
                    this.node.destroy();
                }
            }
        }
        GameUtil.playAnimation(data);
    }
}
// AppService.instance.registViewData(VIEW_DATA);